import axios from 'axios'
import { Message } from 'view-ui-plus';

const instance = axios.create({
    baseURL: 'http://localhost:8083/',
    timeout: 30000,
});

//请求前处理
instance.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
}, function (error) {
    // Do something with request error
    console.log(error)
    Message[error]({
        background: true,
        content: '请求前错误：'+error
    });
    return Promise.reject(error)
});

// 请求后处理
instance.interceptors.response.use(function (response) {
    let res=response.data;
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    if(res === undefined||res == null){
        Message[error]({
            background: true,
            content: '请求错误：返回内容不存在'
        });
    }
    if(res.errno !== 0){
        Message[error]({
            background: true,
            content: '请求错误：'+res.errmsg
        });
    }
    return res.data;
}, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    console.log(error)
    Message[error]({
        background: true,
        content: '请求错误：'+error
    });
    return Promise.reject(error)
});


export default instance;